const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeName: "",
    order_by: "new",
    type: "1",
    myLocation: {},
    classifyData: [],
    dataList: [],
    finished: false,
    page: 1,
    info: false,
    name: "",
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      type: options.type,
      myLocation: JSON.parse(options.myLocation),
      id: options.id
    })
    wx.setNavigationBarTitle({
      title: this.data.type == '1' ? '发布者需求列表' : this.data.type == '2' ? '技能需求列表' : '企业需求列表',
    });
    this.getClassifyData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  clickTypeOp(item) {
    this.setData({
      order_by: item.currentTarget.dataset.name,
      dataList: [],
      page: 1,
      finished: false
    })
    this.getData();
  },
  getClassifyData () {
    let baseUrl = app.getNetAddresss("plugin.be-within-call.frontend.index.get-category-list");
    app._postNetWork({
      url: baseUrl,
      data: {
        sort_type: this.data.type
      },
      success: ({data : { data,result,msg }}) => {
        if (result !== 1) {
          wx.showToast({
            title: msg,
          });
          return false;
        }
        this.setData({
          classifyData: data,
          activeName: this.data.id !== '0' ? this.data.id * 1 : data[0].id * 1
        })
        if(this.data.type == '3') {
          let newArr = this.data.classifyData;
          newArr.unshift({
            id: "",
            title: "全部分类",
          })
          this.setData({ 
            classifyData: newArr
          })
        }
        this.getData();
      }
    });
  },
  getData () {
    if (this.data.finished) return;
    let url = this.data.type == "1" ? 'plugin.be-within-call.frontend.public-search.need-order' : this.data.type == "2" ? 'plugin.be-within-call.frontend.public-search.shifu' : 'plugin.be-within-call.frontend.public-search.enterprise-need'
    let form = {};
    if( this.data.type == "1" ||  this.data.type == "2") {
      form ={
        lng: this.data.myLocation ? this.data.myLocation.point.lng : "",
        lat: this.data.myLocation ? this.data.myLocation.point.lat : "",
        page_size: 15,
        name: "",
        page: this.data.page,
        category_id: this.data.activeName,
        order_by: this.data.order_by
      }
    }else {
      form = {
        name: this.data.name,
        page_size: 15,
        category_id: this.data.activeName,
        page: this.data.page,
      }
    }
    let baseUrl = app.getNetAddresss(url);
    app._postNetWork({
      url: baseUrl,
      data: form,
      success: (respone) => {
        if (respone.data.result !== 1) {
          wx.showToast({
            title: respone.data.msg,
          });
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let dataList = [];
        if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
          finished = true;
        }
        dataList = this.data.dataList.concat(res.data);

        this.setData({
          finished,
          dataList,
          page: ++this.data.page,
          info: true
        });
      }
    });
  },
  onChangeTab(e) {
    this.setData({
      activeName: e.detail.name,
      dataList: [],
      page: 1,
      finished: false
    })
    this.getData();
  },
  goTechnicianHome(e) {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/home/technician_home/index?id=${e.currentTarget.dataset.id}`,
    });
  },
  goividualNeeds(e) {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/home/individual_needs/index?type=${e.currentTarget.dataset.type}&order_id=${e.currentTarget.dataset.id}`,
    });
  },
  onChange(e) {
    this.setData({
      name: e.detail,
    });
  },
  inputValue(e) {
    this.setData({
      dataList: [],
      name: e.type == "clear" ? "" : this.data.name,
      page: 1,
      finished: false
    })
    this.getData();
  },
})